package com.platform.usercenter.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.platform.common.core.utils.DateUtils;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.SysDeptPost;
import com.platform.usercenter.domain.SysOrganizationDept;
import com.platform.usercenter.mapper.SysDeptPostMapper;
import com.platform.usercenter.mapper.SysOrganizationDeptMapper;
import com.platform.usercenter.service.ISysDeptPostService;
import com.platform.usercenter.utils.TreeAdapterUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门岗位Service业务层处理
 *
 * @author platform
 * @date 2024-10-24
 */
@Service
public class SysDeptPostServiceImpl implements ISysDeptPostService {
    @Resource
    private SysDeptPostMapper sysDeptPostMapper;

    @Resource
    private SysOrganizationDeptMapper deptMapper;

    /**
     * 查询部门岗位
     *
     * @param postId 部门岗位主键
     * @return 部门岗位
     */
    @Override
    public SysDeptPost selectSysDeptPostByPostId(Long postId) {
        return sysDeptPostMapper.selectSysDeptPostByPostId(postId);
    }

    /**
     * 查询部门岗位列表
     *
     * @param sysDeptPost 部门岗位
     * @return 部门岗位
     */
    @Override
    public List<SysDeptPost> selectSysDeptPostList(SysDeptPost sysDeptPost) {
        return sysDeptPostMapper.selectSysDeptPostList(sysDeptPost);
    }

    @Override
    public List<SysOrganizationDept> detpTree(SysOrganizationDept dept) {
        List<SysOrganizationDept> organizationDeptList = new ArrayList<>();
        Long orgId = SecurityUtils.getLoginUser().getOrgId();
        dept.setOrgId(orgId);
        dept.setIsDelete(SysUserConstants.N);
        List<SysOrganizationDept> deptList = deptMapper.selectSysOrganizationDeptList(dept);
        if (CollectionUtil.isNotEmpty(deptList)) {
            organizationDeptList = TreeAdapterUtil.buildTree(deptList, SysOrganizationDept.class, null);
        }
        return organizationDeptList;
    }

    /**
     * 新增部门岗位
     *
     * @param sysDeptPost 部门岗位
     * @return 结果
     */
    @Override
    public int insertSysDeptPost(SysDeptPost sysDeptPost) {
        sysDeptPost.setCreateTime(DateUtils.getNowDate());
        return sysDeptPostMapper.insertSysDeptPost(sysDeptPost);
    }

    /**
     * 修改部门岗位
     *
     * @param sysDeptPost 部门岗位
     * @return 结果
     */
    @Override
    public int updateSysDeptPost(SysDeptPost sysDeptPost) {
        sysDeptPost.setUpdateTime(DateUtils.getNowDate());
        return sysDeptPostMapper.updateSysDeptPost(sysDeptPost);
    }

    /**
     * 批量删除部门岗位
     *
     * @param postIds 需要删除的部门岗位主键
     * @return 结果
     */
    @Override
    public int deleteSysDeptPostByPostIds(Long[] postIds) {
        return sysDeptPostMapper.deleteSysDeptPostByPostIds(postIds);
    }

    /**
     * 删除部门岗位信息
     *
     * @param postId 部门岗位主键
     * @return 结果
     */
    @Override
    public int deleteSysDeptPostByPostId(Long postId) {
        return sysDeptPostMapper.deleteSysDeptPostByPostId(postId);
    }
}
